﻿using ApiDoc.Models;
using ApiDoc.Models.BLL;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace ApiDoc.Admin
{
    public class HttpClientApi : HttpClient
    {
        public HttpClientApi()
        {
            string uriString = System.Configuration.ConfigurationManager.AppSettings["BaseAddress"];
            base.BaseAddress = new Uri(uriString);
        }

        #region 文件夹

        public async Task<List<FolderModel>> FolderList(int parentSN)
        {
            List<FolderModel> folders = new List<FolderModel>();

            string requestUri = "/Folder/Query?parentSN=" + parentSN.ToString();
            HttpResponseMessage httpResponseMessage = await base.GetAsync(requestUri);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                folders = await httpResponseMessage.Content.ReadAsAsync<List<FolderModel>>();
            }

            return folders;
        }

        public async Task<List<FolderModel>> FolderListAll()
        {
            List<FolderModel> folders = new List<FolderModel>();

            string requestUri = "/Folder/QueryAll";
            HttpResponseMessage httpResponseMessage = await base.GetAsync(requestUri);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                folders = await httpResponseMessage.Content.ReadAsAsync<List<FolderModel>>();
            }

            return folders;
        }

        public async Task<FolderModel> FolderSave(FolderModel model)
        { 
            HttpResponseMessage httpResponseMessage = await this.Post("/Folder/SaveFolder", model); 
            FolderModel folder = new FolderModel();
            if (httpResponseMessage.IsSuccessStatusCode)
            { 
                folder = await httpResponseMessage.Content.ReadAsAsync<FolderModel>();
            }

            return folder;
        }

        public async Task<int> FolderDelete(int SN)
        {
            int reslut = 0;

            string requestUri = "/Folder/Delete?SN=" + SN.ToString();
            HttpResponseMessage httpResponseMessage = await base.GetAsync(requestUri);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                reslut = await httpResponseMessage.Content.ReadAsAsync<int>();
            }

            return reslut;
        }

        #endregion

        #region 接口基础

        public async Task<List<ApiModel>> InterfaceList(string title, string id)
        {
            string requestUri = $"/Interface/List?title={title}&id={id}";
            HttpResponseMessage httpResponseMessage = await base.GetAsync(requestUri);

            List<ApiModel> interfaces = new List<ApiModel>();
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                interfaces = await httpResponseMessage.Content.ReadAsAsync<List<ApiModel>>();
            }

            return interfaces;
        }

        /// <summary>
        /// 接口保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ApiModel> InterfaceSave(ApiModel model)
        {
            string requestUri = $"/Interface/Save";
            HttpResponseMessage httpResponseMessage = await this.Post(requestUri, model);
             
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                model = await httpResponseMessage.Content.ReadAsAsync<ApiModel>();
            }

            return model;
        }


        //虚拟目录
        public async Task<string> FullPath(int fksn)
        {
            string requestUri = $"/Interface/FullPath?";

            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("fksn", fksn);

            HttpResponseMessage httpResponseMessage = await this.get(requestUri, param);

            string fullPath = "";
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                fullPath = await httpResponseMessage.Content.ReadAsStringAsync();
            }

            return fullPath;
        }

        #endregion

        #region 接口参数
         
        /// <summary>
        /// 接口参数列表
        /// </summary>
        /// <param name="fksn"></param>
        /// <returns></returns>
        public async Task<List<ParamModel>> ParamList(int fksn)
        {
            string requestUri = $"/Param/List?";
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("fksn", fksn);
            HttpResponseMessage httpResponseMessage = await this.get(requestUri, param);
            List<ParamModel> paramList = new List<ParamModel>();
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                paramList = await httpResponseMessage.Content.ReadAsAsync<List<ParamModel>>();
            }

            return paramList;
        }

        /// <summary>
        /// 接口参数保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> ParamSave(ParamModel model)
        {
            string requestUri = $"/Param/Save";
            HttpResponseMessage httpResponseMessage = await this.Post(requestUri, model);

            int result = 0;
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                result = await httpResponseMessage.Content.ReadAsAsync<int>();
            }

            return result;
        }
         
        /// <summary>
        /// 接口参数删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> ParamDelete(List<int> ids)
        {
            string requestUri = $"/Param/Delete";
            string json = JsonConvert.SerializeObject(ids);
            StringContent stringContent = new StringContent(json);
            stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpResponseMessage httpResponseMessage = await base.PostAsync(requestUri, stringContent);

            int result = 0;
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                result = await httpResponseMessage.Content.ReadAsAsync<int>();
            }

            return result;
        }

        #endregion

        #region 接口步骤

        public async Task<StepModel> StepGet(int SN)
        {
            string requestUri = $"/Step/Get?";
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("SN", SN);
            HttpResponseMessage httpResponseMessage = await this.get(requestUri, param);
            StepModel model = new StepModel();
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                model = await httpResponseMessage.Content.ReadAsAsync<StepModel>();
            }

            return model;
        }

        /// <summary>
        /// 接口步骤列表
        /// </summary>
        /// <param name="fksn"></param>
        /// <returns></returns>
        public async Task<List<FlowStepModel>> StepList(int fksn)
        {
            string requestUri = $"/Step/List?";
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("fksn", fksn);
            HttpResponseMessage httpResponseMessage = await this.get(requestUri, param);
            List<FlowStepModel> paramList = new List<FlowStepModel>();
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                paramList = await httpResponseMessage.Content.ReadAsAsync<List<FlowStepModel>>();
            }

            return paramList;
        }
         
        public async Task<int> StepSave(FlowStepModel model)
        {
            string requestUri = $"/Step/Save";
            HttpResponseMessage httpResponseMessage = await this.Post(requestUri, model);

            int result = 0;
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                result = await httpResponseMessage.Content.ReadAsAsync<int>();
            }

            return result;
        }

        public async Task<int> StepDelete(int SN)
        {
            string requestUri = $"/Step/Delete"; 
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("SN", SN);
            HttpResponseMessage httpResponseMessage = await this.get(requestUri, param);
            int result = 0;
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                result = await httpResponseMessage.Content.ReadAsAsync<int>();
            }

            return result;
        }
         
        public async Task<int> StepSaveCmdText(FlowStepModel model)
        {
            string requestUri = $"/Step/Save/CmdText";
            
            HttpResponseMessage httpResponseMessage = await this.Post(requestUri, model);
            int result = 0;
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                result = await httpResponseMessage.Content.ReadAsAsync<int>();
            }

            return result;
        }

        #endregion

        #region 步骤参数


        /// <summary>
        /// 接口参数列表
        /// </summary>
        /// <param name="fksn"></param>
        /// <returns></returns>
        public async Task<List<FlowStepParamModel>> StepParamList(int fksn)
        {
            string requestUri = $"/Step/Param/List?";
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("fksn", fksn);
            HttpResponseMessage httpResponseMessage = await this.get(requestUri, param);
            List<FlowStepParamModel> paramList = new List<FlowStepParamModel>();
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                paramList = await httpResponseMessage.Content.ReadAsAsync<List<FlowStepParamModel>>();
            }

            return paramList;
        }

        /// <summary>
        /// 接口参数保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> StepParamSave(FlowStepParamModel model)
        {
            string requestUri = $"/Step/Param/Save";
            HttpResponseMessage httpResponseMessage = await this.Post(requestUri, model);

            int result = 0;
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                result = await httpResponseMessage.Content.ReadAsAsync<int>();
            }

            return result;
        }

        /// <summary>
        /// 接口参数删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> StepParamDelete(int SN)
        {
            string requestUri = $"/Step/Param/Delete";
            List<int> ids = new List<int>();
            ids.Add(SN);
            string json = JsonConvert.SerializeObject(ids);
            StringContent stringContent = new StringContent(json);
            stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpResponseMessage httpResponseMessage = await base.PostAsync(requestUri, stringContent);

            int result = 0;
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                result = await httpResponseMessage.Content.ReadAsAsync<int>();
            }

            return result;
        }

        #endregion

        private async Task<HttpResponseMessage> Post(string requestUri, BaseModel model)
        {
            string json = JsonConvert.SerializeObject(model);
            StringContent stringContent = new StringContent(json);
            stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpResponseMessage httpResponseMessage = await base.PostAsync(requestUri, stringContent);
            return httpResponseMessage;
        }
         
        private async Task<HttpResponseMessage> get(string requestUri, Dictionary<string,object> param)
        {
            string strParam = "";
            if (param != null)
            {
                foreach (KeyValuePair<string,object> valuePair in param)
                {
                    if (strParam != "")
                    {
                        strParam += "&";
                    }
                    strParam += $"{valuePair.Key}={valuePair.Value}";
                }
            }
            
            HttpResponseMessage httpResponseMessage = await base.GetAsync(requestUri + strParam);
            return httpResponseMessage;
        }

    }
}
